package com.ruoyi.web.controller.map;

import com.alibaba.druid.support.json.JSONUtils;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.lang.Const;
import com.ruoyi.common.lang.Result;
import com.ruoyi.common.utils.http.HttpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("地图数据管理")
@RestController
@RequestMapping("/map/china")
public class ChinaMapController extends MapBaseController {

    @ApiOperation("地图数据信息")
    @GetMapping("/chinaData")
    public Result data(){
        Object object;
        if (redisUtil.hasKey(Const.ChinaData)){
            object = redisUtil.get(Const.ChinaData);
        }else {
            String res = HttpUtils.sendGet("https://c.m.163.com/ug/api/wuhan/app/data/list-total");
            object = JSONUtils.parse(res);
            redisUtil.set(Const.ChinaData, object, 1800);
        }
        return Result.succ(object);
    }

    @ApiOperation("新闻信息")
    @GetMapping("/news")
    public Result news(){
        try {
            String res = HttpUtils.sendGet("https://opendata.baidu.com/data/inner?tn=reserved_all_res_tn&dspName=iphone&from_sf=1&dsp=iphone&resource_id=28565&alr=1&query=%E5%9B%BD%E5%86%85%E6%96%B0%E5%9E%8B%E8%82%BA%E7%82%8E%E6%9C%80%E6%96%B0%E5%8A%A8%E6%80%81");
            JSONObject jsonObject = JSONUtil.parseObj(res);
            Object result = jsonObject.getByPath("Result[0].items_v2[0].aladdin_res.DisplayData.result.items");
            return Result.succ(result);
        }catch (Exception e){
            return Result.fail("最新疫情新闻获取失败");
        }
    }



}
